package edu.bistu.alumnus.web.action.admin;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Controller
public class ContentController {

	@RequestMapping
	public void edit() {
		System.out.println("edit");
	}

	@RequestMapping(method = RequestMethod.POST)
	public void uploadEdit(
			@RequestParam(value = "editor", required = true) String editor) {
		System.out.println(editor);
		System.out.println("uploadEditOK!");
	}

	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public String uploadImageEdit(HttpServletRequest request) {
		
		System.out.println(request.getParameter("id"));

		// 转型为MultipartHttpRequest：
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		// 获得文件：
		MultipartFile file = multipartRequest.getFile("upload");
		// 获得文件名：
		String name = file.getName();
		String name1 = file.getOriginalFilename();
		System.out.println(name+ " " + name1);
		// 获得输入流：
		try {
			BufferedReader input = new BufferedReader(new InputStreamReader(
					file.getInputStream()));
			System.out.println(input.toString());
			input.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 写入文件
		System.out.println("uploadImageEditOK");
		return "<script type=\"text/javascript\">"  +
				"window.parent.CKEDITOR.tools.callFunction(2, '/userfiles/images/Public Folder/中文.jpg', '');window.parent.newFunc();</script>";
	}

}
